package chatserver.tools;



import java.util.*;

public class IpSubnetFilter {

    public static String bitsToMask(int bits)
    {    
        switch(bits)
        {
            case 1:
                return "128.0.0.0 ";
            case 2:
                return "192.0.0.0";
            case 3:
                return "224.0.0.0";
            case 4:
                return "240.0.0.0";            
            case 5:
                return "248.0.0.0";            
            case 6:
                return "252.0.0.0";            
            case 7:
                return "254.0.0.0";            
            case 8:
                return "255.0.0.0";
            case 9:
                return "255.128.0.0";
            case 10:
                return "255.192.0.0";
            case 11:
                return "255.224.0.0";
            case 12:
                return "255.240.0.0";
            case 13:
                return "255.248.0.0";            
            case 14:
                return "255.252.0.0";            
            case 15:
                return "255.254.0.0";            
            case 16:
                return "255.255.0.0";            
            case 17:
                return "255.255.128.0";
            case 18:
                return "255.255.192.0";
            case 19:
                return "255.255.224.0";
            case 20:
                return "255.255.240.0";
            case 21:
                return "255.255.248.0";
            case 22:
                return "255.255.252.0";            
            case 23:
                return "255.255.254.0";            
            case 24:
                return "255.255.255.0";            
            case 25:
                return "255.255.255.128";            
            case 26:
                return "255.255.255.192";
            case 27:
                return "255.255.255.224";
            case 28:
                return "255.255.255.240";
            case 29:
                return "255.255.255.248";
            case 30:
                return "255.255.255.252";
            case 31:
                return "255.255.255.254";
            default:
                return "255.255.255.255";
        }
    }
    
    private static int[] split(String ip) 
    { 
        int[] result = new int[4]; 
        StringTokenizer st = new StringTokenizer(ip, "."); 
        for (int i = 0; i < 4; i++) 
        { 
            result[i] = Integer.parseInt(st.nextToken()); 
        } 
        return result; 
    } 
    public static boolean matches(String visitorIpString, String ipString, String maskString) 
    { 
        int[] vip = split(visitorIpString); 
        int[] ip = split(ipString); 
        int[] mask = split(maskString); 
        for (int i = 0; i < 4; i++) 
        { 
            if ((vip[i] & mask[i]) != ip[i]) 
            { 
                return false; 
            } 
        } 
        return true; 
    } 
    
    public static boolean ipMatches(String ip1, String ip2, int bits)
    {
        return matches(ip1, ip2, bitsToMask(bits));
    }
   
}
